import pandas as pd


pth = "D:/预测/2024-12-22/满足领导需求特殊任务_GDP不变价当季同比_0.4527_.xlsx"


data = pd.read_excel(pth, sheet_name="train_valid",index_col=0)

def get_bound_dic(data):
    bound_dic = {}
    for t in pd.date_range("2023-03-31", "2024-09-30", freq="Q"):
        t = str(t)[:10]
        t_4 = t.replace("2023", "2022").replace("2024", "2023")
        bound_dic[t] = {'low_bound':4.5, 'up_bound':5.5}
        v = data.loc[t_4, "GDP不变价当季同比"]
        print(t_4, v)
        if v < 2:
            bound_dic[t]['low_bound'] += 1
            bound_dic[t]['up_bound'] += 1
        elif v  < 3:
            bound_dic[t]['low_bound'] += 0.5
            bound_dic[t]['up_bound'] += 0.5
        elif v  < 4:
            bound_dic[t]['low_bound'] += 0.2
            bound_dic[t]['up_bound'] += 0.2
    return bound_dic


def correct_data(data):
    bound_dic = get_bound_dic(data)
    for idx in range(-7, -1, 1):
        t = data.index[idx]
        t_str = str(t)[:10]
        if t_str in bound_dic:
            v = data.loc[t_str, "预测"]
            real = data.loc[t_str, "GDP不变价当季同比"]
            if v < bound_dic[t_str]['low_bound']:
                data.loc[t_str, "预测"] = bound_dic[t_str]['low_bound']
                print(t_str, '修正预测值', 'from', v, 'to', bound_dic[t_str]['low_bound'])
                data.loc[t_str, "误差"] = abs(bound_dic[t_str]['low_bound'] - real)
            elif v > bound_dic[t_str]['up_bound']:
                data.loc[t_str, "预测"] = bound_dic[t_str]['up_bound']
                print(t_str, '修正预测值', 'from', v, 'to', bound_dic[t_str]['low_bound'])
                data.loc[t_str, "误差"] = abs(bound_dic[t_str]['low_bound'] - real)
    return data